package ru.agiledev.aos.analyzer;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import ru.agiledev.aos.commons.dto.SemanticQuery;
import ru.agiledev.aos.data.HibernateUtil;
import ru.agiledev.aos.data.domain.SetComponent;

import java.util.List;

/**
 * Class contains methods for fully semantic and domain-specific analysis
 *
 * Created by a.kirillov
 * Date: 11/18/11 Time: 3:33 PM
 */
public class SemanticAnalyzer {


    /**
     * Methods returns number of level of word in partOf hierarchy company->department->group->team (1->2->3->4)
     * @param word string representation of term
     * @return number of level if found, -1 in other case
     */
    public int getTermLevelInDomainHierarchy(String word){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(SetComponent.class).add(Restrictions.eq("value", word));
        List<SetComponent> list = criteria.list();

        if(list.size()>0){
            int result = list.get(0).getValue().length();
            session.close();
            return result;
        }
        session.close();

        return -1;
    }

    //method for transformation String NL-query to semantically extended
    public SemanticQuery transformQuery(String query){
        QueryAnalyzer analyzer = new QueryAnalyzer();
        SemanticQuery semanticQuery = analyzer.getOI(query);

        if(semanticQuery.isSearchReady()){
            QueryExtender queryExtender = new QueryExtender();
            queryExtender.extendQuery(semanticQuery);
        }

        return semanticQuery;
    }
}
